package com.sale.succ.forms;

import static com.sale.succ.utils.TraderHelper.getLabel;

import java.io.File;
import java.io.IOException;
import java.util.Date;

import com.sale.succ.components.DefForm;
import com.sale.succ.components.DefLabel;
import com.sale.succ.components.MessDialog;
import com.sale.succ.objects.Session;
import com.sale.succ.system.TradeMarketException;
import com.sale.succ.system.ftp.FTPException;
import com.sale.succ.system.ftp.FTPProcessing;
import com.sale.succ.system.txt.ObjectsProvider;
import com.sale.succ.system.txt.TXTProcessing;
import com.sale.succ.utils.DateFormatter;
import com.sale.succ.utils.SystemConstants;
import com.sale.succ.utils.TraderConstants;
import com.sale.succ.utils.TraderHelper;

import eve.fx.Insets;
import eve.fx.gui.WindowConstants;
import eve.ui.Application;
import eve.ui.Button;
import eve.ui.CellPanel;

public class MainForm extends DefForm {
	private CellPanel mainPanel;
	/**
	 * download button
	 */
	private Button downloadButton;
	/**
	 * exit
	 */
	private Button exitButton;
	/**
	 * Enter
	 */
	private Button enterButton;
	/**
	 * upload button
	 */
	private Button uploadButton;
	/**
	 * help button
	 */
	private Button helpButton;
	/**
	 * option button
	 */
	private Button optionButton;
	
	private MessDialog loadingDialog;

	public MainForm() {
		// fullScreenOnPDA();
		title = TraderHelper.getLabel(TraderConstants.MAIN_TITLE);
		addLast(getMainPanel());
		windowFlagsToSet |= WindowConstants.FLAG_MAXIMIZE;
	}

	/**
	 * Main Panel of the Application
	 * 
	 * @return main panel
	 */
	protected CellPanel getMainPanel() {
		if (mainPanel == null) {
			mainPanel = new CellPanel();
			mainPanel.defaultTags.set(TAG_INSETS, new Insets(5, 5, 5, 5));

			DefLabel helloLabel = new DefLabel(TraderConstants.TRADE_MARK);

			mainPanel.addLast(helloLabel).setControl(HCENTER).setCell(HGROW);

			mainPanel.addNext(getEnterButton()).setControl(HGROW);
			mainPanel.addNext(getDownloadButton()).setControl(HGROW);
			mainPanel.addLast(getUploadButton()).setControl(HGROW);
			mainPanel.addNext(getHelpButton()).setControl(HGROW);
			mainPanel.addNext(getOptionButton()).setControl(HGROW);
			mainPanel.addNext(getExitButton()).setControl(HGROW);
		}
		return mainPanel;
	}

	protected Button getDownloadButton() {
		if (downloadButton == null) {
			downloadButton = new Button("", SystemConstants.DEFAULT_PATH
					+ SystemConstants.DOWNLOAD_BUTTON);
			downloadButton.action = TraderConstants.DOWNLOAD_ACTION;
		}
		return downloadButton;
	}

	protected Button getExitButton() {
		if (exitButton == null) {
			exitButton = new Button("", SystemConstants.DEFAULT_PATH
					+ SystemConstants.EXIT_BUTTON);
			exitButton.action = TraderConstants.EXIT_ACTION;
		}
		return exitButton;
	}

	protected Button getEnterButton() {
		if (enterButton == null) {
			enterButton = new Button("", SystemConstants.DEFAULT_PATH
					+ SystemConstants.START_BUTTON);
			enterButton.action = TraderConstants.SALES_ORD_ACTION;
		}
		return enterButton;
	}

	protected Button getUploadButton() {
		if (uploadButton == null) {
			uploadButton = new Button("", SystemConstants.DEFAULT_PATH
					+ SystemConstants.UPLOAD_BUTTON);
			uploadButton.action = TraderConstants.UPLOAD_ACTION;
		}
		return uploadButton;
	}

	protected Button getHelpButton() {
		if (helpButton == null) {
			helpButton = new Button("", SystemConstants.DEFAULT_PATH
					+ SystemConstants.HELP_BUTTON);
			helpButton.action = TraderConstants.HELP_ACTION;
		}
		return helpButton;
	}

	protected Button getOptionButton() {
		if (optionButton == null) {
			optionButton = new Button("", SystemConstants.DEFAULT_PATH
					+ SystemConstants.OPTION_BUTTON);
			optionButton.action = TraderConstants.OPTION_ACTION;
		}
		return optionButton;
	}

	public boolean handleAction(String action) {
		if (action.equals(TraderConstants.DOWNLOAD_ACTION)) {
			loadingDialog = new MessDialog(getLabel(TraderConstants.LOADING), MessDialog.NOB);
			loadingDialog.exec();
			try {
				ObjectsProvider.getInstance().clearCollections();
				Session.getInstance().clearAllMarks();
				String zipName = DateFormatter.dateTimeToString(new Date(), "") + "_" + SystemConstants.ORDER_ZIP;
				FTPProcessing ftp;
				try {
					ftp = new FTPProcessing(Session.getInstance().getLogin(), Session.getInstance().getPassword());
					ftp.setLocalDir(SystemConstants.TRADE_ZIP_DIR);
					ftp.ftpDownload(SystemConstants.TRADE_ZIP);
					if(TXTProcessing.createZip(SystemConstants.ORDERS_DIR + File.separator + zipName, SystemConstants.ORDERS_DIR)){
						ftp.setLocalDir(SystemConstants.ORDERS_DIR);
						ftp.ftpUpload(zipName);
						TXTProcessing.deleteOrderFiles(SystemConstants.ORDERS_DIR);
					}
					ftp.closeFTP();
					File file = new File(SystemConstants.TRADE_ZIP_DIR + File.separator + SystemConstants.RESTS_FILE);
					if(file.exists()){
						file.delete();
					}
					ObjectsProvider.getInstance().getTradeMarkCollection();
					ObjectsProvider.getInstance().getProductCollection();
				} catch (FTPException ex) {
					new TradeMarketException(ex.getMessage(), TraderHelper.getLabel(TraderConstants.FTP_DOWNLOAD_ABORTED));
				} catch (IOException ex) {
					new TradeMarketException(ex.getMessage(), TraderHelper.getLabel(TraderConstants.FTP_DOWNLOAD_ABORTED));
				}
			}
			finally {
				loadingDialog.close(0);
			}
			return true;
		}
		
		if (action.equals(TraderConstants.UPLOAD_ACTION)) {
			loadingDialog = new MessDialog(getLabel(TraderConstants.LOADING), MessDialog.NOB);
			loadingDialog.exec();
			try {
				String zipName = DateFormatter.dateTimeToString(new Date(), "") + "_" + SystemConstants.ORDER_ZIP;
				if(TXTProcessing.createZip(SystemConstants.ORDERS_DIR + File.separator + zipName, SystemConstants.ORDERS_DIR)){
					FTPProcessing ftp;
					try {
						ftp = new FTPProcessing(Session.getInstance().getLogin(), Session.getInstance().getPassword());
						ftp.setLocalDir(SystemConstants.ORDERS_DIR);
						ftp.ftpUpload(zipName);
						ftp.closeFTP();
						TXTProcessing.deleteOrderFiles(SystemConstants.ORDERS_DIR);
					} catch (FTPException ex) {
						new TradeMarketException(ex.getMessage(), TraderHelper.getLabel(TraderConstants.FTP_UPLOAD_ABORTED));
					} catch (IOException ex) {
						new TradeMarketException(ex.getMessage(), TraderHelper.getLabel(TraderConstants.FTP_UPLOAD_ABORTED));
					}
				}
			}
			finally {
				loadingDialog.close(0);
			}
			return true;
		}
		
		if (action.equals(TraderConstants.EXIT_ACTION)) {
			Application.exit(0);
			return true;
		}

		if (action.equals(TraderConstants.SALES_ORD_ACTION)) {
			new RegionForm().execute();
			return true;
		}

		if (action.equals(TraderConstants.OPTION_ACTION)) {
			new SystemPropsForm().execute();
			return true;
		}
		
		if (action.equals(TraderConstants.HELP_ACTION)) {
			new HelpForm().execute();
			return true;
		}
		
		return super.handleAction(action);
	}

	public boolean canExit(int code) {
		return true;
	}
}
